﻿var collapsedLeftPanel = false;
var tabCounter = 1;
var tabData = new Hashtable();
var activeTabKey = null;
var windowBlocked = false;
var hideAlertTimerID = null
var emailAlertTimerID = null
var startReminderTimer = null
var endReminderTimer = null
var emailAlertInterval = 60 * 1000
var reminderAlertInterval = 60 * 1000
var emailAlertDetailLevel = 1
var arrRefFunctions = new Array()
$(function(){	
	/*Add first tab to Dashboard*/
 // var homeText = $("#__HomeText").val()

//  appendTab('icon-dashboard', 'dashboard', "Dashboard", 'calendar/default.aspx', true, null, 1, 99)
//  appendTab('icon-dashboard', 'contact', "Dashboard", 'security/roles/default.aspx', true, null, 1, 99)
  //emailAlertTimerID = setTimeout("Begin_EmailAlert()", 1000)
  //DESPUES EL QUE HACE REMINDER DEBE DE ACTIVAR ESTE CODIGO
  //iReminder()
  //startReminder()
  
  
});
/*******************************************************/
function collapseLeftPanel(action) {    
  if(!($("#left-panel-collapse-min-icons").hasClass("hide"))){
      $("#left-panel-south").height(224);
      $("#left-panel-collapse-min-icons").addClass("hide");
      $("#left-panel-collapse-normal-icons").removeClass("hide");
      $("#Divleft-panel-collapse-bar").addClass("left-panel-collapse-bar").removeClass("left-panel-show-bar");
      $("#divLeftPanel").height($("#divLeftPanel").height() - 224);
  } else {
      $("#left-panel-south").height(28);
      $("#divLeftPanel").height($("#divLeftPanel").height() + 224);
      $("#left-panel-collapse-min-icons").removeClass("hide");
      $("#left-panel-collapse-normal-icons").addClass("hide");
      $("#Divleft-panel-collapse-bar").addClass("left-panel-show-bar").removeClass("left-panel-collapse-bar");
  }
}
/*******************************************************/
function startReminder(){
 if (startReminderTimer !== null){
  window.clearTimeout(startReminderTimer)
 }
 startReminderTimer = window.setTimeout("iReminder(false)", reminderAlertInterval)
}
/*******************************************************/
function iReminder(show){
  forceShowReminder = show
  DoCallBack("GetReminders", "", End_Reminder)
}
/*******************************************************/
function Begin_EmailAlert(){
  DoCallBack("GetEmailAlert", emailAlertDetailLevel, End_EmailAlert)
}
/*******************************************************/
function End_EmailAlert(arg){
  var mData = arg.split(":::")
  
  if (mData[0] !== "0"){
    if (windowBlocked){       
     window.setTimeout(function(){End_EmailAlert(arg)}, 3000)  
     return
    }
    
    var divAlert = document.getElementById("email-alert-box")

    divAlert.style.display = ""


    var lnkAlert = document.getElementById("lnkAlert")

    lnkAlert.innerHTML = mData[1]


    if (emailAlertDetailLevel == 2){
      lnkAlert.href = "javascript:OpenItem('" + mData[2] + "', '" + mData[3] + "');restartAlert()"
    }else{
      lnkAlert.href = "javascript:OpenInbox('Inbox', true);restartAlert()"
    }

    window.setTimeout(function(){
    
      $("#email-alert-box").animate({ 
          height: "116px"
        }, 500 );
    }, 50)
    
     playSound() 
  
    hideAlertTimerID = setTimeout("hideAlert()", 7000)	    
  }else{    
   startAlert()     
  }
  
  emailAlertDetailLevel = 2
}
/*******************************************************/
function End_Reminder(arg){
 var _arg = arg.split(":::")
 
 if (windowBlocked){     
  if (endReminderTimer !== null){
   window.clearTimeout(endReminderTimer)
  }          
  endReminderTimer = window.setTimeout(function(){End_Reminder(arg)}, 3000)  
  return
 }

 if (_arg[0]=="1"){
 
 var mAWidthTask=500;
 
 try{
  var brTask=detectBrowser();
  if(brTask=="SF"){
   mAWidthTask=510;
  }
 }catch(e){}
 
  window.parent.OpenPopup("system/reminder/Default.aspx?dNowUtc=" + _arg[1] + "&dNow=" + _arg[2], document.getElementById("ReminderTitle").value, mAWidthTask, 330, true)
 }else if(forceShowReminder){
  var mAWidthTask=500;
  
  try{
   var brTask=detectBrowser();
   if(brTask=="SF"){
    mAWidthTask=510;
   }
  }catch(e){}   
  window.parent.OpenPopup("system/reminder/Default.aspx", document.getElementById("ReminderTitle").value, mAWidthTask, 330, true)      
 }else{    
  
 }
 startReminder()
}

/*******************************************************/
function hideAlert(){
  
  $("#email-alert-box").animate({ 
      height: "0px"
    }, 500 );
  
  window.setTimeout(function(){
  parent.document.getElementById("email-alert-box").style.display = "none"
  }, 600)
  setTimeout("startAlert()", 4000) 
  
}
/*******************************************************/
function restartAlert(){
 stopAlert()
 startAlert()
}
/*******************************************************/
function closeAlert(){
 parent.document.getElementById("email-alert-box").style.display = "none"
}
/*******************************************************/
function stopAlert(){
 if (hideAlertTimerID !== null){
   window.clearTimeout(hideAlertTimerID)
   hideAlertTimerID = null
 }
 
 if (emailAlertTimerID !== null){
   window.clearTimeout(emailAlertTimerID)
   emailAlertTimerID = null
 }
   
 parent.document.getElementById("email-alert-box").style.display = "none"
}
/*******************************************************/
function startAlert(){
  emailAlertTimerID = setTimeout("Begin_EmailAlert()", emailAlertInterval)
}
/****************************************************/
function playSound(){
  var so = new FlashObject("library/swf/sound.swf", "sound", "0", "0", "8", "#ffffff");
  so.addParam("menu", "false");
  so.addVariable("snd", "library/sound/Sound1.mp3");
  so.write("soundObject")
}  

/*************************************************/
function setMenu(key){
  $("#panel-menu-options .x-menu").removeClass("menu-active")
  $("#panel-menu-options #" + key).addClass("menu-active")
}
/*************************************************/
function setMenuSmall(key){
  $("#panel-menu-options .x-menu").removeClass("menu-active")
  $("#panel-menu-options #" + key).addClass("menu-active")
}
/*************************************************/
function activeTab(key){
  var tabIndex = tabData.get(key)[0]
  var title = tabData.get(key)[2]
  var leftPanelIndex = tabData.get(key)[3]
  
  activeTabKey = key
  $("#tab-list li").removeClass("x-tab-active")
  $(".frame-container").css("display", "none")

  $("#tab-list #tab-" + tabIndex).addClass("x-tab-active")
  $("#frame-" + tabIndex).css("display", "")
  
  panelBar(leftPanelIndex, key, title);
  window.setTimeout(function(){SetLeftPanelOption(leftPanelIndex, key, null);}, 100);
}
/*************************************************/
function closeTab(key, parentKey, close){

  if(tabData.get(key) == null){
    return false
  }

  var tabIndex = tabData.get(key)[0]
  var contentWindow = document.getElementById("frame-" + tabIndex).contentWindow
  
  if (contentWindow.WindowClose && !close){
    activeTab(key)
    contentWindow.WindowClose()
  }else{
    $("#tab-list #tab-" + tabIndex).remove()
    $("#frame-" + tabIndex).remove()
    tabData.remove(key)

    if (parentKey && tabData.containsKey(parentKey)){
      activeTab(parentKey)
    }else if (tabData.keys().length !== 0){
      activeTab(tabData.keys()[0])
    }
  }
}
/*************************************************/
function getActiveContent(){
  var tabIndex = tabData.get(activeTabKey)[0]
  return document.getElementById("frame-" + tabIndex).contentWindow
  
}
/*************************************************/
function tabRename(newText){
  var tabIndex = tabData.get(activeTabKey)[0]
  
  $("#tab-list #tab-" + tabIndex + " .x-tab-text").text(newText)
  
}
/*************************************************/
function getContent(tabKey){
  if(tabData.get(tabKey)!==null){
    var tabIndex = tabData.get(tabKey)[0]
    return document.getElementById("frame-" + tabIndex).contentWindow
  }else{return null}
}
/*************************************************/
function getFrameObject(tabKey){
  if(tabData.get(tabKey)!==null){
    var tabIndex = tabData.get(tabKey)[0]
    return document.getElementById("frame-" + tabIndex)
  }else{return null}
}
/*************************************************/
function closeActiveTab(close){
  var parentKey = tabData.get(activeTabKey)[1]
  closeTab(activeTabKey, parentKey, close)
}
/*************************************************/
function tabCloseFocus(){
  closeActiveTab(true);
}
/*************************************************/
function panelProcess(){
  $.blockUI({ message: '<img src="../library/css/skins/neutral/images/ajax-loader.gif" /> <h1>Processing. Please Wait...</h1>' });
}
/*************************************************/
function panelHidden(){
  $.unblockUI();

}
/*************************************************/
function panelLoanding(){
  $.blockUI({ message: '<img src="../library/css/skins/neutral/images/ajax-loader.gif" /> <h1>Loading. Please Wait...</h1>' });
}
/*************************************************/
function panelUpdatingView(){
  $.blockUI({ message: '<img src="../library/css/skins/neutral/images/ajax-loader.gif" /> <h1>Loading. Please Wait...</h1>' });
}

/*************************************************/
function appendTab(tabClass, key, text, url, notClosable, parentKey, timeout, leftPanelIndex){
  

  if (tabData.get(key) == null) {  
    panelLoanding()
    if (!timeout)
      timeout = 2000     
  }else{
    timeout = 1
  }
  
  window.setTimeout(function(){
    var tabIndex = null;
    if (tabData.get(key) == null) {  
      
      tabCounter++;
      tabIndex = tabCounter
      tabData.put(key.valueOf(), [tabIndex, parentKey, text, leftPanelIndex]);
      var q = String.fromCharCode(34)
      
      var htmlTab = "<li class='"
      if (!notClosable) htmlTab += "x-tab-closable "
      
      
      if (tabClass) htmlTab += "x-tab-with-icon "
      htmlTab += "x-tab-active' id='tab-" + tabIndex + "'>"
      htmlTab += "<a onclick='closeTab(" + q + key + q + ", " + q + parentKey + q + ")' class='x-tab-close'></a>"
      
      htmlTab += "<span onclick='activeTab(" + q + key + q + ")' class='x-tab-icon " + tabClass + "'></span>"
      htmlTab += "<a class='x-tab-left'><em class='x-tab-right'><span  class='x-tab-inner'><span class='x-tab-text' onclick='activeTab(" + q + key + q + ")'>" + text + "</span></span></em></a></li>"
      $("#tab-list li").removeClass("x-tab-active")
      $(".frame-container").css("display", "none")
      
      $("#tab-list").append(htmlTab)
      //url = "about:blank"
      $("#main-container").append("<iframe class='frame-container' id='frame-" + tabIndex + "' frameborder=0 width='100%' height='100%' src='" + url + "'></iframe>")
    }else{
      tabIndex = tabData.get(key)

      $("#tab-list li").removeClass("x-tab-active")
      $(".frame-container").css("display", "none")
      
      $("#tab-list #tab-" + tabIndex).addClass("x-tab-active")
      $("#frame-" + tabIndex).css("display", "")
    }
    activeTabKey = key;        
  }, timeout)
  
  //panelBar(leftPanelIndex, key, text)
  
  ///window.setTimeout(function(){SetLeftPanelOption(leftPanelIndex, null, url)}, 1500);
}
/*************************************************/
function customPopUp(_url,_width,_height,_center,_resizable){
  day = new Date();
  id = day.getTime();
  var StringEvalPopUp;

  StringEvalPopUp="page"+id+"=window.open(_url,'HTML','toolbar=0,scrollbars=0";
  if(_center){
   StringEvalPopUp+=",top="+((screen.height - _height) / 2)+",left="+((screen.width - _width) / 2); 
  }

  StringEvalPopUp+=",location=0,status=0,menubar=0,resizable="+_resizable+",width="+_width+",height="+_height+"');";
  var a=eval(StringEvalPopUp);

  return a;
}
/***************************************************************************************/
function SendParams(fid,params){
	arrRefFunctions[fid](params);
}
/***************************************************************************************/
var leftPanelSize = new Array();
/***************************************************************************************/
function panelBar(i, pos, panelTitle){

//   var _URL = "about:blank"
//   switch (i){
//   /*******************************************************************************/
//    case 2: // Left Panel Contact
//     _URL ="";// "system/contact/leftpanel.aspx?idx=" + pos
//     break;
//   }
//  
//  //  hidden all left panel...
////  $("#divLeftPanel .left-container").addClass("hide")
//// 
////  if ($("#left-panel-" + i).length == 0){
////    $("#divLeftPanel").append("<iframe class='left-container' id='left-panel-" + i + "' frameborder=0 width='100%' height='99%' src='" + _URL + "'></iframe>") 
////  }else{
////    $("#left-panel-" + i).removeClass("hide")
////  }
}
/****************************************************************************************************/
function SetLeftPanelOption(LeftPanelIndex, key, url){
  var lpObject = parent.document.getElementById("left-panel-" + LeftPanelIndex);
  
  try{
    if(lpObject.contentWindow.setOption && getContent(key) !== null){
      window.setTimeout(function(){
        if(key!==null){
          url = getContent(key).location.href
        }
        lpObject.contentWindow.setOption(url);  
      }, 500);
    }
  }catch(ex){}
}
